package com.china08.yxyapi.api.manage.sch.platform.xiaoben.catalog;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolCatalog;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolCatalogRepository;

/**
 * 校本课程--删除一级目录
 * 
 * @author LiuWeidong
 *
 */

@CrossOrigin
@RestController
public class XiaobenCatalogOneDelController extends YxyApiBaseController {

    @Autowired
    private YxyLessonsSchoolCatalogRepository yxyLessonsSchoolCatalogRepository;

    @RequestMapping(value = "/api_yw/manage/sch/platform/xiaoben/catalog/one/del", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel model) {

        String idOne = model.idOne;
        List<YxyLessonsSchoolCatalog> yxyLessonsSchoolCatalogLst = yxyLessonsSchoolCatalogRepository.findByPid(idOne);
        Assert.isTrue(yxyLessonsSchoolCatalogLst.size() == 0, "有子分类，请先删除子分类！");

        YxyLessonsSchoolCatalog yxyLessonsSchoolCatalogs = yxyLessonsSchoolCatalogRepository.findById(idOne);
        if (yxyLessonsSchoolCatalogs != null) {
            yxyLessonsSchoolCatalogRepository.delete(yxyLessonsSchoolCatalogs);
        }
    }

    /** 请求参数 */
    protected static class ReqModel extends YxyApiReqModel {

        // 一级分类ID
        public String idOne;

    }
}
